﻿Module Backup_Restore

    Public Sub BackupDatabase(ByVal filename As String)
        Dim tabel As String = "tbl_pendaftaran"
        Dim rcd As Odbc.OdbcDataReader
        Dim field_count As Integer
        Dim i As Integer
        Dim out As String = vbNullString
        Dim tmpRcd As String = vbNullString
        Dim typeData As String

        rcd = Connection.ExecuteToDataReader("SELECT * FROM " & tabel)

        If rcd.HasRows Then
            field_count = rcd.FieldCount - 1

            While rcd.Read
                out &= "INSERT INTO " & tabel & " VALUES ("
                tmpRcd = vbNullString

                For i = 0 To field_count
                    typeData = rcd.Item(i).GetType.ToString
                    'tmpRcd &= "T:" & rcd.Item(i).GetType.ToString & " "
                    Select Case typeData
                        Case "System.Int32"
                            tmpRcd &= rcd.Item(i).ToString & ", "
                        Case "System.String"
                            tmpRcd &= "'" & StringSafe(rcd.Item(i).ToString) & "', "
                        Case "System.Single"
                            tmpRcd &= FormatNumber(rcd.Item(i).ToString, 2).Replace(",", ".") & ", "
                        Case "System.DateTime"
                            Dim dt As Date = rcd.Item(i).ToString
                            tmpRcd &= "'" & dt.ToString("yyyy-MM-dd") & "', "
                        Case Else
                            If IsNumeric(rcd.Item(i).ToString) Then
                                tmpRcd &= rcd.Item(i).ToString & ", "
                            Else
                                tmpRcd &= "'" & StringSafe(rcd.Item(i).ToString) & "', "
                            End If
                    End Select

                Next

                tmpRcd = tmpRcd.Substring(0, tmpRcd.Length - 2)
                out &= tmpRcd & ");" & vbCrLf
            End While

            My.Computer.FileSystem.WriteAllText(filename, out, False, System.Text.Encoding.ASCII)
            MsgBox("Proses Backup Berhasil dilakukan...", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Pesan")
            'My.Computer.Clipboard.SetText(out)
            'MsgBox("tada")
        Else
            MsgBox("Tidak Ada data untuk di-backup!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Pesan")
        End If

    End Sub

    Private Function StringSafe(ByVal tmpRcd As String) As String
        tmpRcd = tmpRcd.Replace("\", "\\")
        tmpRcd = tmpRcd.Replace("'", "\'")
        tmpRcd = tmpRcd.Replace(Chr(13), "\r\n")

        Return tmpRcd
    End Function

    Public Function RestoreDatabase(ByVal filename As String) As Boolean
        Try

            ' Clear data
            Connection.ExecuteNonQuery("TRUNCATE TABLE `tbl_pendaftaran`")

            Dim Str As String = My.Computer.FileSystem.ReadAllText(filename)
            Dim StrSql() As String = Str.Split(vbCrLf)
            Dim i As Integer

            If Str.IndexOf("tbl_pendaftaran") <> -1 Then

                For i = 0 To StrSql.Length - 1
                    If StrSql(i).IndexOf("INSERT INTO") <> -1 Then
                        If Connection.ExecuteNonQuery(StrSql(i)) = False Then
                            Return False
                        End If
                    End If
                Next

            Else
                MsgBox("File SQL tidak berlaku!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Pesan")
                Return False
            End If

        Catch ex As Exception
            MsgBox("Error Restore : " & vbCrLf & ex.Message, MsgBoxStyle.Exclamation, "Pesan")
            Return False
        End Try

        Return True
    End Function

End Module
